package com.huadao.cart_ccs.data.bo;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.common.ByteUtils;
import com.huadao.cart_ccs.common.TimeUtil;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;

public class DeviceBO extends BaseBO {
    private byte[] MAC;
    private byte[] agentNumber;
    private byte[] ip;
    private byte[] netmask;
    private Date date;

    public static final byte ModularType_DeviceMAC = 0x0;
    public static final byte deviceMAC_NO1 = 01;//设备MAC
    public static final byte deviceMAC_NO2 = 02;//时间
    public static final byte deviceMAC_SHUTDOWN = 03;//关机
    public static final byte read = (byte) 0xFF;//读
    public static final byte modify = (byte) 0xBB;//修改
    public static final byte shutdown = (byte) 0xBB;//关机

    public DeviceBO() {
    }

    public byte[] getMAC() {
        return MAC;
    }

    public void setMAC(byte[] MAC) {
        this.MAC = MAC;
    }

    public byte[] getAgentNumber() {
        return agentNumber;
    }

    public void setAgentNumber(byte[] agentNumber) {
        this.agentNumber = agentNumber;
    }

    public byte[] getIp() {
        return ip;
    }

    public void setIp(byte[] ip) {
        this.ip = ip;
    }

    public byte[] getNetmask() {
        return netmask;
    }

    public void setNetmask(byte[] netmask) {
        this.netmask = netmask;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    protected String getOrderStr() {
        String orderStr = null;
        if(modularNO==deviceMAC_NO1||modularNO==deviceMAC_NO2){
            if (order == read) {
                orderStr = CartApplication.getInstance().getString(R.string.read);
            } else if (order == modify) {
                orderStr = CartApplication.getInstance().getString(R.string.modify);
            }
        }else if(modularNO==deviceMAC_SHUTDOWN){
            if(order==shutdown){
                orderStr=CartApplication.getInstance().getString(R.string.shutdown);
            }
        }
        return orderStr;
    }

    @Override
    protected String getOrderContentStr() {
        StringBuilder stringBuilder = new StringBuilder();
        if (modularNO == deviceMAC_NO1) {
            if (MAC != null && MAC.length > 0) {
                String macStr = ByteUtils.getHexByte(MAC, 0, MAC.length);
                stringBuilder.append("MAC:");
                stringBuilder.append(macStr);
            }
            if (agentNumber != null && agentNumber.length == 2) {
                stringBuilder.append("代理商编号:");
                stringBuilder.append(String.format("%2d", ByteUtils.byte2int(agentNumber[0])));
                stringBuilder.append(String.format("%02d", ByteUtils.byte2int(agentNumber[1])));
            }
            if (ip != null && ip.length == 4) {
                stringBuilder.append("IP:");
                stringBuilder.append(ByteUtils.byte2int(ip[0]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(ip[1]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(ip[2]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(ip[3]));
            }
            if (netmask != null && netmask.length == 4) {
                stringBuilder.append("IP:");
                stringBuilder.append(ByteUtils.byte2int(netmask[0]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(netmask[1]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(netmask[2]));
                stringBuilder.append(".");
                stringBuilder.append(ByteUtils.byte2int(netmask[3]));
            }
        }else if(modularNO==deviceMAC_NO2){
            if(date!=null) {
                stringBuilder.append(TimeUtil.device_time_sdf.format(date));
            }
        }else if(modularNO==deviceMAC_SHUTDOWN){

        }
        return stringBuilder.toString();
    }

    @Override
    protected String getModularNOStr() {
        if (modularNO == deviceMAC_NO1) {
            return CartApplication.getInstance().getString(R.string.mac);
        }else if(modularNO==deviceMAC_NO2){
            return CartApplication.getInstance().getString(R.string.time);
        }else if(modularNO==deviceMAC_SHUTDOWN){
            return CartApplication.getInstance().getString(R.string.shutdown);
        }
        return super.getModularNOStr();
    }

    @Override
    protected String getModularTypeStr() {
        return CartApplication.getInstance().getString(R.string.devices);
    }

}
